package com.atzzy.ysmall.ysmall_productserviceimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atzzy.ysmall.ysmall_allService.ProductService.ShoppingCartServices;
import com.atzzy.ysmall.ysmall_beans.ShoppingCart;
import com.atzzy.ysmall.ysmall_beans.ShoppingCartVo;
import com.atzzy.ysmall.ysmall_common.vo.R;
import com.atzzy.ysmall.ysmall_mapper.ShoppingsCartMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author JAVAZeng
 * @Date 2022/3/8 14:25
 * @Version 1.0
 */
@Service
@Component
public class ShoppingCartServiceImpl implements ShoppingCartServices {

    @Resource
    private  ShoppingsCartMapper shoppingCartMapper;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    @Override
    public R addShoppingCart(ShoppingCart shoppingCart) {

        shoppingCart.setCartTime(sdf.format(new Date()));
        Integer integer = shoppingCartMapper.updateShoppingCart(shoppingCart);
        if(integer>0)
        {
            return new R(201,"success",null);
        }
        return new R(501,"fail",null);
    }

    @Override
    public R selectShoppingCart(Integer userId) {
        List<ShoppingCartVo> shoppingCartVos = shoppingCartMapper.selectShopcartByUserId(userId);
        return new R(201,"success",shoppingCartVos);
    }

    @Override
    public R updateCartNum(Integer cartId, Integer cartNum) {Integer result = shoppingCartMapper.updateCartNumByCarid(cartId, cartNum);
        if(result>0)
        {
            return new R(201,"success",null);
        }else{
            return new R(501,"fail",null);
        }
    }

    @Override
    public R deleteCart(Integer cartId) {
        Integer integer = shoppingCartMapper.deleteCartByCarId(cartId);
        System.out.println(cartId);
        if(integer>0)
        {
            return new R(201,"success",null);
        }
        return new R(501,"fail",null);
    }

    @Override
    public R listShoppingCartsByCids(String cids) {
        String[] arr = cids.split(",");
        List<Integer> cartIds = new ArrayList<>();
        for(int i = 0;i<arr.length;i++)
        {
            cartIds.add(Integer.parseInt(arr[i]));
        }
        List<ShoppingCartVo> shoppingCartVos = shoppingCartMapper.selectShopCartByCids(cartIds);
        return new R(201,"success",shoppingCartVos);
    }

}
